#!/usr/bin/env python

__author__ = "JT Olds <hello@jtolds.com>"

import os, re, sys

def generate(dir_name):
  NON_ALPHANUM = re.compile(r'[^a-zA-Z0-9]')

  assets = {}
  for asset in os.listdir(dir_name):
    assets[NON_ALPHANUM.sub('_', asset.upper())] = \
        file(os.path.join(dir_name, asset)).read()

  header = file("%s.h" % dir_name, "w")
  header.write("#ifndef __%s_H__\n" % dir_name.upper())
  header.write("#define __%s_H__\n\n" % dir_name.upper())
  header.write("// THIS FILE WAS AUTOGENERATED BY generate_assets.py %s\n\n" %
      dir_name)
  header.write("namespace pants {\nnamespace %s {\n\n" % dir_name)
  for name in assets.keys():
    header.write("extern const char* %s;\n" % name)
  header.write("\n}}\n\n#endif\n")
  header.close()

  body = file("%s.cpp" % dir_name, "w")
  body.write("#include \"%s.h\"\n\n" % dir_name)
  body.write("// THIS FILE WAS AUTOGENERATED BY generate_assets.py %s\n\n" %
      dir_name)
  for name, data in assets.iteritems():
    body.write("const char* pants::%s::%s = " % (dir_name, name))
    for i, char in enumerate(data):
      if i == 0: body.write("\n  \"")
      body.write("\\x%02x" % ord(char))
      if i % 18 == 17: body.write("\"\n  \"")
    body.write("\\x00\";\n\n")


if __name__ == "__main__":
  generate(sys.argv[1])
